package com.fph.design.util;

import java.util.*;

/**
 * 打乱选型，并记录正确答案
 * @ClassName TopicRandomUtil
 * @Description TODO
 * @Author fph
 * @Date 2024-08-06 10:22
 **/
public class TopicRandomUtil {

    /**
     * 乱序Map元素，记录对应答案key
     * @param option 题目
     * @param key    答案
     * @return Topic 乱序后 {A=c., B=d., C=a., D=b.}
     */
    static public Topic random(Map<String, String> option, String key) {
        Set<String> keySet = option.keySet();
        ArrayList<String> keyList = new ArrayList<String>(keySet);
        //乱序
        Collections.shuffle(keyList);
        //新的选项
        HashMap<String, String> optionNew = new HashMap<String, String>();
        int index = 0;
        String ketNew = "";
        for (String next : keySet){
            String randomKey = keyList.get(index++);
            if (key.equals(next)) {
                ketNew = randomKey;
            }
            optionNew.put(randomKey, option.get(next));
        }
        return new Topic(option, ketNew);
    }
}

